<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <!-- <el-form-item label="关键字" prop="keyword">
                    <el-input v-model.trim="queryParams.keyword" placeholder="请输入关键字" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="订单号" prop="order_id">
                    <el-input type="textarea" :rows="4" v-model="queryParams.order_id" placeholder="请输入订单号" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="订单编号" prop="name">
                    <el-input v-model.trim="queryParams.name" placeholder="请输入订单编号" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="支付状态" prop="financial_status">
                    <el-select v-model="queryParams.financial_status" placeholder="请选择支付状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in FINANCIAL_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="物流状态" prop="fulfillment_status">
                    <el-select v-model="queryParams.fulfillment_status" placeholder="请选择物流状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in FULFILLMENT_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="月份" prop="date">
                    <el-date-picker type="month" v-model="queryParams.date" :disabled="disabled"></el-date-picker>
                </el-form-item> -->
                <el-form-item label="时间" prop="createtime">
                    <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="会话标识" prop="slug" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="客户" prop="author" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="时间" prop="created_at" min-width="" show-overflow-tooltip></el-table-column>
                <el-table-column label="状态" prop="status" min-width="" show-overflow-tooltip></el-table-column>
                <!-- <el-table-column label="操作" min-width="160" fixed="right">
                    <template slot-scope="scope">
                        <el-button el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">查看详情</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <!-- <feedback-dialog v-model="feedbackDialog.visible" v-if="feedbackDialog.visible" :params="feedbackDialog.data" @success="handleQuery"></feedback-dialog> -->
    </div>
</template>

<script>
import { queryReamazeList, exportReamaze } from '@/api/order'
import { getShopList } from '@/api/shop'
import { STATUS_LIST, FINANCIAL_STATUS, FULFILLMENT_STATUS } from './constant'
import { exportFile } from '@/utils/index'
import DetailDialog from './components/DetailDialog.vue'
import { parseTime } from '@/utils/quickadmin'
// import FeedbackDialog from './components/FeedbackDialog.vue'
export default {
    name: 'OrderList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            FINANCIAL_STATUS,
            FULFILLMENT_STATUS,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                createtime: undefined,
                order_id: undefined,
                shop_id: undefined,
                name: undefined,
                financial_status: undefined,
                fulfillment_status: undefined,
                create_time: [],
                keyword: undefined,
            },
            detailDialog: {
                visible: false,
                data: {},
                type: ''
            },
            feedbackDialog: {
                visible: false,
                data: {},
            },
            shopList: [],
        }
    },
    async created() {
        this.loading = true
        // try {
        //     const result = await getShopList()
        //     this.shopList = result?.data?.list ?? []
        // } catch (error) {
        //     console.log(error)
        //     this.loading = false
        // }
        this.getList()
    },
    methods: {
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            exportReamaze(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        // showFeedbackDialog(row) {
        //     this.feedbackDialog.visible = true
        //     this.feedbackDialog.data = row
        // },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date !== undefined) {
                qyparams.date = parseTime(qyparams.date, 'y-m')
            }
            if (qyparams.createtime?.length == 2) {
                qyparams.createtime = [parseTime(qyparams.createtime[0], 'y-m-d'), parseTime(qyparams.createtime[1], 'y-m-d')]
            }
            queryReamazeList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
